import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
//import qt.control.manager 1.0
Window {
    id: window
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    signal initDisplay(type:int, id:string, text:string, cmd:string);
    signal sendCmd(cmdStr:string)

    onSendCmd: (cmd) => {
                   console.log("执行命令:    " + cmd)
               }

    onInitDisplay: (type, id, text, cmd) => {
                       console.log("id =" + id + ", text =" + text)
                       if (type === 0) {  // 创建按钮
                           var newButtonCreate = Qt.createComponent("MyDymButton.qml")

                           var newButton = newButtonCreate.createObject(grid)
                           newButton.text = text
                           newButton.myId = id
                           newButton.cmdStr = cmd
                           newButton.Layout.fillHeight = true
                           newButton.Layout.fillWidth = true
                           newButton.sendCmd.connect(window.sendCmd)

                       }
                   }

    Connections {
        target: manager
    }
    Rectangle {
        id: root
        anchors.fill: parent

        function createNewButton(name, cmd) {
            var mcol, mrow
            if (grid.curElementNum === 0) {
                mcol = 1
                mrow = 1
                grid.curElementNum++
            }

            var newButtonCreate = Qt.createComponent("MyDymButton.qml")

            var newButton = newButtonCreate.createObject(grid)

            newButton.text = name
            newButton.cmdStr = cmd
            newButton.Layout.fillHeight = true
            newButton.Layout.fillWidth = true
            newButton.sendCmd.connect(window.sendCmd)
            contextMenu.saveNewButtonToJson(newButton)
            grid.curElementNum++
        }

        GridLayout {
            id: grid
            anchors.fill: parent
            columns: 4
            rows:3
            focus: true
            property var curElementNum: 0
            MyDymButton {
                text: "参考按钮"
                cmdStr: "CKAN"
                Layout.fillHeight: true
                Layout.fillWidth: true
            }

            Component.onCompleted: {
                manager.loadJson()
            }

        }

        MouseArea {
            id: mouseRoot
            anchors.fill: parent
            acceptedButtons: Qt.RightButton
            onPressed: (mouse) =>{
                           if (mouse.button === Qt.RightButton) {
                               contextMenu.popup()
                           }
                       }
        }

        Dialog {
                id: dialogButton
                title: "设置按钮名字和功能缩写"
                anchors.centerIn: parent
                width: 300
                height: 200

                contentItem:Row {
                    anchors.centerIn: parent
                    spacing: 20
                    Rectangle {
                        width: 100
                        height: 20
                        TextField {
                            id: name
                            text: "按钮名字"
                        }
                    }
                    Rectangle {
                        width: 100
                        height: 20
                        TextField {
                            id: cmd
                            text: "功能缩写"
                        }
                    }

                }

                standardButtons: Dialog.Ok | Dialog.Cancel

                onAccepted: {
                    root.createNewButton(name.text, cmd.text)
                }

                onRejected: {
                    console.log("Cancel button clicked")
                }
            }

        Menu {
            id: contextMenu
            width:100
            // height:80
            property var name: "button"
            property var buttonCount: 0
            property var file: "../../loadJson/comp1.json"
            function saveNewButtonToJson(button) {
                var bKey = contextMenu.name + buttonCount
                var ele = {}
                ele[bKey] = {
                    text : button.text,
                    relComp : "",
                    cmd : button.cmdStr
                }

                var stateString = JSON.stringify(ele);
                console.log("stateString =" + stateString)
                manager.saveJson(file, bKey, stateString)
                buttonCount++;

            }

            MenuItem {
                text: "按钮"
                onTriggered: {
                    dialogButton.open()

                }
            }

            MenuItem {
                text: "按钮/输入"
                onTriggered: {

                }
            }
        }
    }
    Component.onCompleted: {
        manager.initDisplay.connect(window.initDisplay)
    }
}
